package com.kazalan.content.document.bl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.kazalan.content.persistence.DocumentDao;
import com.kazalan.content.persistence.LanguageDao;
import com.kazalan.domain.document.Document;
import com.kazalan.domain.document.DocumentAggregate;
import com.kazalan.domain.document.Language;

public class DocumentServiceImpl implements DocumentService {

	private static Logger log = Logger.getLogger(DocumentServiceImpl.class);

	@Autowired
	private DocumentDao documentDao;

	@Autowired
	private LanguageDao languageDao;

	@Override
	public void addDocument(Document document) {

		DocumentAggregate documentAggregate = new DocumentAggregate();
		document.setDocumentAggregate(documentAggregate);
		documentAggregate.addDocument(document);

		this.documentDao.saveDocumentAggregate(documentAggregate);
	}

	@Override
	public Document getDocumentById(String id) {
		return this.documentDao.getDocumentById(id);
	}

	@Override
	public void saveDocument(Document document) {
		this.documentDao.saveDocument(document);

	}

	@Override
	public Map<String, Language> getLanguages() {
		
		Map<String, Language> languages = new HashMap<String, Language>();
		
		for (Language language : this.languageDao.getLanguages()) {
			languages.put(language.getCode(), language);
		}
		
		return languages;
	}
	
	@Override
	public Language getLanguageByCode(String code) {
		return this.languageDao.getLanguageByCode(code);
	}

	@Override
	public DocumentAggregate getDocumentAggregateById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	public void setDocumentDao(DocumentDao documentDao) {
		this.documentDao = documentDao;
	}

	public void setLanguageDao(LanguageDao languageDao) {
		this.languageDao = languageDao;
	}

}
